/****

    activequant - activestocks.eu

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

	
	contact  : contact@activestocks.eu
    homepage : http://www.activestocks.eu

****/
package org.activequant.util.charting;

import org.activequant.core.domainmodel.account.BalanceBook;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.time.Millisecond;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;

/**
 * 
 * Creates a line chart from a balance book. Merely a helper class. <br>
 * EquityChart extends Chart.<br/>
 * Holds the following inherited variables:
 * <ul>
 * <li>chart(JFreeChart)</li>
 * <li>dataset(TimeSeriesCollection)</li>
 * <li>drawColor(Color)</li>
 * </ul>
 * <b>History:</b><br>
 *  - [15.07.2007] Created (Ulrich Staudinger)<br>
 *  - [08.12.2007] Fixing rendering of charts <br>  
 *
 *  @author Ulrich Staudinger
 */
public class EquityChart extends Chart {
	/**
	 * Constructs a EquityChart(extends Chart) whose inherited chart(JFreeChart) is a time series chart.<br/>
	 * A time series chart is an {@link XYPlot} with a {@link DateAxis} for the x-axis and a  {@link NumberAxis} for the y-axis.  The default renderer is an {@link XYLineAndShapeRenderer}. <P>
     * A convenient dataset to use with this chart is a  {@link org.jfree.data.time.TimeSeriesCollection}.<br/>
     * The inherited chart(JFreeChart) has no title(String), empty timeAxisLabel(String) and valueAxisLabel(String), has no dataset set, a legend is required but no tooltips and URLs.
	 */
	public EquityChart() {
		super();
	}

	/**
	 * sets the equity chart on this object, but does not render it. Call renderToXYZ to actually render it.<br/>
	 * Iterates the balanceEntries(List&lt;BalanceEntry&gt;) of the given balanceBook(BalanceBook) and looks at the stamp(TimeStamp) and value(double)
	 * of each BalanceEntry. Calculates a cumulative value and adds an "Equity curve" at index 0 of the XYPlot of the inherited chart(JFreeChart) using the cumulative value - timeStamp pairs.
	 * @param balanceBook
	 */
	public void createEquityChart(BalanceBook balanceBook) {
		final TimeSeries ts = new TimeSeries("Equity curve", Millisecond.class);
		double value = 0.0;
		for (org.activequant.core.domainmodel.account.BalanceEntry be : balanceBook.getBalanceEntries()) {
			value += be.getValue();
			ts.addOrUpdate(new Millisecond(be.getTimeStamp().getDate()), value);
		}
		final TimeSeriesCollection dataset = new TimeSeriesCollection(ts);
		final XYPlot plot1 = chart.getXYPlot();
		plot1.setDataset(0, dataset);
	}
}
